package javaCode;

import java.awt.*;

/**
 * Created by lxy_920912 on 23/04/2017.
 */
public class HeapSort {
    public void BuildHeap(int[]arr){
        int len = arr.length;
        for(int i = len/2;i >= 0;i--){
            HeapAdjust(arr,i,len);
        }

    }
    public void HeapAdjust(int[] arr,int i,int size){
        int leftChild = 2*i+1;
        int rightChild = 2*i+2;
        int max = i;
        if( i <= size/2){
            if(leftChild <= size && arr[leftChild] > arr[max]){
                max = leftChild;
            }
            if(rightChild <= size && arr[rightChild] > arr[max]){
                max = rightChild;
            }
            if(max != i){
                int temp = arr[max];
                arr[max] = arr[i];
                arr[i] = temp;
                HeapAdjust(arr,max,size);
            }

        }

    }
    public void HeapSort(int []arr){
        BuildHeap(arr);
        for(int i = arr.length-1; i >= 0;i--){
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;
            HeapAdjust(arr,1,i-1);
        }
    }

}
//对堆进行初始化
//调节筛选